//
//  ViewController.m
//  UI10_homework(TableViewCellSelfSize)
//
//  Created by Hanlingyu on 16/7/27.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "CarTableViewCell.h"
#import "CarNewsObject.h"
#import "UIView+Frame.h"
#import "LTQCycleImages.h"
#import "CycleImageObject.h"
#define kTableViewCellReuse @"reuse"

@interface ViewController ()

<
    UITableViewDataSource,
    UITableViewDelegate
>

@property (nonatomic, strong) UITableView *carTableView;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *cycleArray;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationController.navigationBarHidden = YES;
    
    [self handleSource];
    [self createTableView];
    _carTableView.tableHeaderView = [self creatTableViewHeaderView];
    
}

// 创建tableView
- (void)createTableView {
    self.carTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _carTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_carTableView];
    
    _carTableView.delegate = self;
    _carTableView.dataSource = self;
    
    [_carTableView registerClass:[CarTableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
    _carTableView.rowHeight = 100;
}

// 创建tableView的头视图
- (UIView *)creatTableViewHeaderView {
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.ltq_width, 300)];
    // 轮播图创建
    LTQCycleImages *cycleImages = [[LTQCycleImages alloc] initWithFrame:CGRectMake(0, 0, self.view.ltq_width, 200)];
    [cycleImages setImagesWithArray:_cycleArray];
    [backgroundView addSubview:cycleImages];
    
    // 四个button和label
    NSArray *imageNameArray = @[@"1", @"2", @"3", @"4"];
    NSArray *labelContentArray = @[@"贷款买车", @"直销车型", @"低价买车", @"二手车"];
    for (int i = 0; i < 4; i++) {
        UIButton *carButton = [UIButton buttonWithType:UIButtonTypeCustom];
        carButton.frame = CGRectMake(27 + 97 * i, 200, 70, 70);
        [carButton setImage: [UIImage imageNamed:imageNameArray[i]] forState:UIControlStateNormal];
        [backgroundView addSubview:carButton];
        
        UILabel *carLabel = [[UILabel alloc] initWithFrame:CGRectMake(27 + 97 * i, 270, 70, 30)];
        carLabel.text = labelContentArray[i];
        carLabel.textAlignment = 1;
        [backgroundView addSubview:carLabel];
    }
    return backgroundView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.carNew = _listArray[indexPath.row];
    return cell;
}

// 对数据源进行处理分别加入两个数组中
- (void)handleSource {
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *totalDic = [NSMutableDictionary dictionaryWithContentsOfFile:dataPath];
    NSDictionary *dataDic = [totalDic objectForKey:@"data"];
    NSMutableArray *cycleImage = [dataDic objectForKey:@"cycleImage"];
    self.cycleArray = [NSMutableArray array];
    for (NSDictionary *dic in cycleImage) {
        CycleImageObject *cycleImage = [[CycleImageObject alloc] init];
        [cycleImage setValuesForKeysWithDictionary:dic];
        // 通过网络请求, 得到data, 如果图片不正确data为nil,
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:cycleImage.picCover]];
        // 当data不为nil时, 将data转化成UIImage, 并将其存入事先初始化好了的属性数组中
        if (data != nil) {
            UIImage *image = [UIImage imageWithData:data];
            [self.cycleArray addObject:image];
        }
    }
    
    NSMutableArray *listArray = [dataDic objectForKey:@"list"];
    self.listArray = [NSMutableArray array];
    for (NSDictionary *dic in listArray) {
        CarNewsObject *carNew = [[CarNewsObject alloc] init];
        [carNew setValuesForKeysWithDictionary:dic];
        [_listArray addObject:carNew];        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
